﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class NewsBLL
    {
        string pc = "vvvxxx!@#$abc*()";
        public NewsCls CreateModel(string ActiveLanguage, string NewsId)
        {
            return CreateModel(Environments.ConnectionString, ActiveLanguage, NewsId);
        }

        public NewsCls CreateModel(string ConnectionString, string ActiveLanguage, string NewsId)
        {
            string Query = "select NewsId,[" + ActiveLanguage + "Subject],belongSite,[" + ActiveLanguage + "ShortContent],FriendlyTitle," +
                           "    [" + ActiveLanguage + "Body]," +
                           "    [" + ActiveLanguage + "EcryptBody]," +
                           "    [" + ActiveLanguage + "ecryptShortContent]," +
                           "    [" + ActiveLanguage + "ecryptSubject]," +
                           "    [Owner]," +
                           "    [ImageUrl]," +
                           "    [OriginId]," +
                           "    [fk_GroupNews_Id]," +
                           "    [fk_GroupNewsItem_Id]," +
                           "    [SearchKeyword], " +
                           "    [Source]," +
                           "    [IsPublishing]," +
                           "    [HasDocAttached]," +
                           "    [HasImageAttached], " +
                           "    [HasVideoAttached], " +
                           "    EmbVideo, " +
                           "    EmbAudio, " +
                           "    [EntryDate] " +
                           "  from TableNews where NewsId=@NewsId";
            SqlConnection sqlConnection = new SqlConnection(ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                NewsCls NewsCls = new NewsCls();
                NewsCls.NewsId = (string)dtResult.Rows[0]["NewsId"];
                try
                {
                    NewsCls.Subject = (string)dtResult.Rows[0][ActiveLanguage + "Subject"];
                }
                catch { }
                try
                {
                    NewsCls.belongSite = (string)dtResult.Rows[0]["belongSite"];
                }
                catch { }

                try
                {
                    NewsCls.ShortContent = (string)dtResult.Rows[0][ActiveLanguage + "ShortContent"];
                }
                catch { }

                NewsCls.ShortContent = System.Web.HttpUtility.HtmlDecode(NewsCls.ShortContent);


                try
                {
                    NewsCls.FriendlyTitle = (string)dtResult.Rows[0]["FriendlyTitle"];
                }
                catch { }
                try
                {
                    NewsCls.Body = (string)dtResult.Rows[0][ActiveLanguage + "Body"];
                }
                catch { }

                try
                {
                    NewsCls.ecryptBody = new RSACSPCls().Decrypt(pc, (string)dtResult.Rows[0][ActiveLanguage + "EcryptBody"]);
                }
                catch { }

                try
                {
                    NewsCls.ecryptShortContent = new RSACSPCls().Decrypt(pc, (string)dtResult.Rows[0][ActiveLanguage + "EcryptShortContent"]);
                }
                catch { }

                try
                {
                    NewsCls.ecryptSubject = new RSACSPCls().Decrypt(pc, (string)dtResult.Rows[0][ActiveLanguage + "Subject"]);
                }
                catch { }

                try
                {
                    NewsCls.Owner = (string)dtResult.Rows[0]["Owner"];
                }
                catch { }

                try
                {
                    NewsCls.EmbAudio = (string)dtResult.Rows[0]["EmbAudio"];
                }
                catch { NewsCls.EmbAudio = string.Empty; }

                try
                {
                    NewsCls.EmbVideo = (string)dtResult.Rows[0]["EmbVideo"];
                }
                catch { NewsCls.EmbVideo = string.Empty; }

                try
                {
                    NewsCls.ImageUrl = (string)dtResult.Rows[0]["ImageUrl"];
                }
                catch { }
                try
                {
                    NewsCls.OriginId = (string)dtResult.Rows[0]["OriginId"];
                }
                catch { }
                try
                {
                    NewsCls.OriginUrl = (string)dtResult.Rows[0]["OriginUrl"];
                }
                catch { }

                try
                {
                    NewsCls.GroupNewsId = (string)dtResult.Rows[0]["fk_GroupNews_Id"];
                }
                catch { }

                try
                {
                    NewsCls.GroupNewsItemId = (string)dtResult.Rows[0]["fk_GroupNewsItem_Id"];
                }
                catch { }

                try
                {
                    NewsCls.SearchKeyword = (string)dtResult.Rows[0]["SearchKeyword"];
                }
                catch { }

                try
                {
                    NewsCls.Source = (string)dtResult.Rows[0]["Source"];
                }
                catch { }

                try
                {
                    NewsCls.IsPublishing = (int)dtResult.Rows[0]["IsPublishing"];
                }
                catch { }

                try
                {
                    NewsCls.HasDocAttached = (int)dtResult.Rows[0]["HasDocAttached"];
                }
                catch { }

                try
                {
                    NewsCls.HasImageAttached = (int)dtResult.Rows[0]["HasImageAttached"];
                }
                catch { }

                try
                {
                    NewsCls.HasVideoAttached = (int)dtResult.Rows[0]["HasVideoAttached"];
                }
                catch { }

                NewsCls.EntryDate = (DateTime)dtResult.Rows[0]["EntryDate"];
                return NewsCls;
            }
        }

        public DataTable ReadingForSync(string belongSite, string GroupNewsItemId)
        {
            string Query = "select * from TableNews where belongSite=@belongSite and fk_GroupNewsItem_Id=@GroupNewsItemId and IsPublishing=1";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@belongSite", SqlDbType.VarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;

            sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }



        public DataTable ReadingForSyncFromTool(string ConnectionString, string belongSite)
        {
            string Query = "select NewsId from TableNews where belongSite=@belongSite and IsPublishing=1";
            SqlConnection sqlConnection = new SqlConnection(ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@belongSite", SqlDbType.VarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public DataTable ReadingHistory(string NewsId)
        {
            string Query = "select * from TableNewsHistory where fk_News_Id=@NewsId order by LogDate asc ";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable Reading(
            string belongSite,
            string GroupNewsId,
            string GroupNewsItemId,
            int WorkflowStatusIndex)
        {
            string Query = "select NewsId,TotalNewsAttachments=(select count(NewsAttachsId) from TableNewsAttachmentsDetail where fk_NewsId=NewsId),TotalNewsRelation=(select count(NewsRelationId) from TableNewsRelation where fk_SrcNews_Id=NewsId),belongSite,Subject=IsNull(Subject,''),[" + Environments.ActiveLanguage + "Subject],EntryDate,fk_GroupNews_Id,fk_GroupNewsItem_Id,WorkFlowStatus,Owner,ShortId,OriginUrl,OriginId,TotalView,ImageUrl,IsPublishing,SmallImage,SearchKeyword,FriendlyTitle,ShowOnTop,Source,UploadFileName,[" + Environments.ActiveLanguage + "EcryptSubject] from TableNews where belongSite='" + belongSite + "' and  fk_GroupNews_Id='" + GroupNewsId + "' ";
            if (WorkflowStatusIndex != -1)
            {
                Query += " and IsPublishing=" + WorkflowStatusIndex;
            }
            if (GroupNewsItemId != null)
            {
                Query += " and fk_GroupNewsItem_Id='" + GroupNewsItemId + "'";
            }
            Query += " order by ShowOnTop DESC, EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable ReadingNews_Hot(            
            string belongSite,
            int WorkflowStatusIndex)
        {
            string Query = " select * from TableNews where " +
                            " belongSite = @belongSite and " +
                            " ShowOnTop =1 ";
            if (WorkflowStatusIndex != -1)
            {
                Query += " and IsPublishing=" + WorkflowStatusIndex;
            }   
            Query += " order by EntryDate DESC ";

            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;

            sqlCommand.Parameters.Add("@belongSite", SqlDbType.VarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;

            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public DataTable ReadingForRss(string GroupNewsItemId, int TOP)
        {
            string Query = "select TOP " + TOP + " NewsId,ShortId, belongSite,Subject=IsNull(Subject,''),[" + Environments.ActiveLanguage + "Subject],EntryDate,ShortContent=[" + Environments.ActiveLanguage + "Subject] from TableNews where fk_GroupNewsItem_Id='" + GroupNewsItemId + "'" +
                           "    order by ShowOnTop DESC, EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable Searching(
                string belongSite,
                string Keyword,
                string fromDate,
                string toDate,
                int IsPublishing,
                string Source,
                string GroupNewsItemId
            )
        {
            string Query = " select NewsId, GroupNewsId=TableGroupNewsItem.fk_GroupNews_Id, GroupNewsItemId=TableNews.fk_GroupNewsItem_Id, [" + Environments.ActiveLanguage + "Subject],IsPublishing, EntryDate, WorkFlowStatus, ShowOnTop, belongSite = TableNews.BelongSite, ShortId = TableNews.ShortId, UploadFileName,TotalNewsRelation=(select count(NewsRelationId) from TableNewsRelation where fk_SrcNews_Id=NewsId) from TableNews " +
                           "    left outer join TableGroupNewsItem on TableGroupNewsItem.GroupNewsItemId=fk_GroupNewsItem_Id" +
                           "    left outer join TableGroupNewsItem as p on p.GroupNewsItemId=TableGroupNewsItem.fk_ParentGroupNewsItem_Id" +
                           " where TableNews.belongSite = '" + belongSite + "'";
            if (Keyword.Trim().Length > 0)
            {
                Query += " and (SearchKeyword like N'%" + Keyword + "%' or [" + Environments.ActiveLanguage + "Subject] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "ShortContent] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "Body] like N'%" + Keyword + "%' )";
            }
            if (fromDate.Trim().Length > 0)
            {
                Query += " and EntryDate between '" + Environment.WebPartUtilities.VNDateToDate(fromDate).ToString("MM/dd/yyyy 00:00") + "' and '" + Environment.WebPartUtilities.VNDateToDate(toDate).ToString("MM/dd/yyyy 23:59") + "'";
            }

            if (IsPublishing != -1)
            {
                Query += " and IsPublishing=" + IsPublishing;
            }
            if (Source.Trim().Length > 0)
            {
                Query += " and Source like N'%" + Source + "%'";
            }
            if (GroupNewsItemId.Trim().Length > 0)
            {
                Query += " and (TableGroupNewsItem.GroupNewsItemId='" + GroupNewsItemId + "' or p.GroupNewsItemId='" + GroupNewsItemId + "')";
            }
            Query +=
                   " order by EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public DataTable SearchingForRelation(
                string SrcNewsId,
                string belongSite,
                string Keyword,
                string fromDate,
                string toDate,
                int IsPublishing,
                string Source,
                string GroupNewsItemId
            )
        {
            string Query = " select NewsId, GroupNewsId=TableGroupNewsItem.fk_GroupNews_Id, GroupNewsItemId=TableNews.fk_GroupNewsItem_Id, [" + Environments.ActiveLanguage + "Subject],IsPublishing, EntryDate, WorkFlowStatus, ShowOnTop, belongSite = TableNews.BelongSite, ShortId = TableNews.ShortId, UploadFileName,TotalNewsRelation=(select count(NewsRelationId) from TableNewsRelation where fk_SrcNews_Id=NewsId) from TableNews " +
                           "    left outer join TableGroupNewsItem on TableGroupNewsItem.GroupNewsItemId=fk_GroupNewsItem_Id" +
                           "    left outer join TableGroupNewsItem as p on p.GroupNewsItemId=TableGroupNewsItem.fk_ParentGroupNewsItem_Id" +
                           " where TableNews.belongSite = '" + belongSite + "' and NewsId not in (select fk_RelationNews_Id from TableNewsRelation where fk_SrcNews_Id='" + SrcNewsId + "')";
            if (Keyword.Trim().Length > 0)
            {
                Query += " and (SearchKeyword like N'%" + Keyword + "%' or [" + Environments.ActiveLanguage + "Subject] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "ShortContent] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "Body] like N'%" + Keyword + "%' )";
            }
            if (fromDate.Trim().Length > 0)
            {
                Query += " and EntryDate between '" + Environment.WebPartUtilities.VNDateToDate(fromDate).ToString("MM/dd/yyyy 00:00") + "' and '" + Environment.WebPartUtilities.VNDateToDate(toDate).ToString("MM/dd/yyyy 23:59") + "'";
            }

            if (IsPublishing != -1)
            {
                Query += " and IsPublishing=" + IsPublishing;
            }
            if (Source.Trim().Length > 0)
            {
                Query += " and Source like N'%" + Source + "%'";
            }
            if (GroupNewsItemId.Trim().Length > 0)
            {
                Query += " and (TableGroupNewsItem.GroupNewsItemId='" + GroupNewsItemId + "' or p.GroupNewsItemId='" + GroupNewsItemId + "')";
            }
            Query +=
                   " order by EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public DataTable SearchingForFocusNews(
                string FocusNewsId,
                string belongSite,
                string Keyword,
                string fromDate,
                string toDate,
                int IsPublishing,
                string Source,
                string GroupNewsItemId
            )
        {
            string Query = " select NewsId, GroupNewsId=TableGroupNewsItem.fk_GroupNews_Id, GroupNewsItemId=TableNews.fk_GroupNewsItem_Id, [" + Environments.ActiveLanguage + "Subject],IsPublishing, EntryDate, WorkFlowStatus, ShowOnTop, belongSite = TableNews.BelongSite, ShortId = TableNews.ShortId, UploadFileName,TotalNewsRelation=(select count(NewsRelationId) from TableNewsRelation where fk_SrcNews_Id=NewsId) from TableNews " +
                           "    left outer join TableGroupNewsItem on TableGroupNewsItem.GroupNewsItemId=fk_GroupNewsItem_Id" +
                           "    left outer join TableGroupNewsItem as p on p.GroupNewsItemId=TableGroupNewsItem.fk_ParentGroupNewsItem_Id" +
                           " where TableNews.belongSite = '" + belongSite + "' and NewsId not in (select fk_News_Id from TableFocusNewsItems where fk_FocusNews_Id='" + FocusNewsId + "')";
            if (Keyword.Trim().Length > 0)
            {
                Query += " and (SearchKeyword like N'%" + Keyword + "%' or [" + Environments.ActiveLanguage + "Subject] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "ShortContent] like N'%" + Keyword + "%'  or [" + Environments.ActiveLanguage + "Body] like N'%" + Keyword + "%' )";
            }
            if (fromDate.Trim().Length > 0)
            {
                Query += " and EntryDate between '" + Environment.WebPartUtilities.VNDateToDate(fromDate).ToString("MM/dd/yyyy 00:00") + "' and '" + Environment.WebPartUtilities.VNDateToDate(toDate).ToString("MM/dd/yyyy 23:59") + "'";
            }

            if (IsPublishing != -1)
            {
                Query += " and IsPublishing=" + IsPublishing;
            }
            if (Source.Trim().Length > 0)
            {
                Query += " and Source like N'%" + Source + "%'";
            }
            if (GroupNewsItemId.Trim().Length > 0)
            {
                Query += " and (TableGroupNewsItem.GroupNewsItemId='" + GroupNewsItemId + "' or p.GroupNewsItemId='" + GroupNewsItemId + "')";
            }
            Query +=
                   " order by EntryDate DESC";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable ReadingWorkflowStatus(string GroupNewsId)
        {
            string Query = " Update TableNews set WorkflowStatus='New' where WorkflowStatus is null;" +
                           " select distinct WorkflowStatus from TableNews where fk_GroupNews_Id='" + GroupNewsId + "'";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public DataTable ReadingWorkflowStatus()
        {
            string Query = "select distinct WorkflowStatus from TableNews";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public string AddNews(
            SqlTransaction sqlTransaction,
            string ActiveLanguage,
            string Subject,
            string ImageUrl,
            string ShortContent,
            string Body,
            string GroupNewsId,
            string GroupNewsItemId,
            string OriginId,
            string OriginUrl,
            string Owner,
            string SearchKeyword,
            string Source,
            DateTime EntryDate,
            int HasDocAttached
            //int HasVideoAttached,
            //int HasImageAttached,
            //string EmbVideo,
            //string EmbAudio
            )
        {
            GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string NewsId = System.Guid.NewGuid().ToString();
            string FriendlyTitle = Environments.ConvertSubject(Subject);
            string Query = "Insert into TableNews(NewsId,belongSite,ShortId,[" + ActiveLanguage + "Subject],ImageUrl,[" + ActiveLanguage + "ShortContent],[" + ActiveLanguage + "Body],fk_GroupNews_Id,fk_GroupNewsItem_Id,OriginId,OriginUrl, Owner,SearchKeyword,FriendlyTitle,Source,[" + ActiveLanguage + "EcryptBody],[" + ActiveLanguage + "EcryptShortContent],[" + ActiveLanguage + "EcryptSubject],EntryDate, HasDocAttached) values (@NewsId,@belongSite,@ShortId,@Subject,@ImageUrl,@ShortContent,@Body,@fk_GroupNews_Id,@fk_GroupNewsItem_Id,@OriginId,@OriginUrl, @Owner,@SearchKeyword,@FriendlyTitle,@Source,@EcryptBody,@EcryptShortContent,@EcryptSubject,@EntryDate, @HasDocAttached )";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            sqlCommand.Parameters.Add("@belongSite", SqlDbType.NVarChar);
            sqlCommand.Parameters["@belongSite"].Value = GroupNewsCls.belongSite;

            sqlCommand.Parameters.Add("@ShortId", SqlDbType.VarChar);
            sqlCommand.Parameters["@ShortId"].Value = NewsId.Substring(0, 8);

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@ImageUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@ImageUrl"].Value = ImageUrl;


            sqlCommand.Parameters.Add("@ShortContent", SqlDbType.NText);
            sqlCommand.Parameters["@ShortContent"].Value = ShortContent;

            sqlCommand.Parameters.Add("@Body", SqlDbType.NText);
            sqlCommand.Parameters["@Body"].Value = Body;

            sqlCommand.Parameters.Add("@fk_GroupNews_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_GroupNews_Id"].Value = GroupNewsId;

            sqlCommand.Parameters.Add("@fk_GroupNewsItem_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_GroupNewsItem_Id"].Value = GroupNewsItemId;

            sqlCommand.Parameters.Add("@OriginId", SqlDbType.NVarChar);
            if (OriginId == null)
            {
                sqlCommand.Parameters["@OriginId"].Value = DBNull.Value;
            }
            else
            {
                sqlCommand.Parameters["@OriginId"].Value = OriginId;
            }

            sqlCommand.Parameters.Add("@OriginUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@OriginUrl"].Value = OriginUrl;

            sqlCommand.Parameters.Add("@Owner", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Owner"].Value = Owner;

            sqlCommand.Parameters.Add("@SearchKeyword", SqlDbType.NVarChar);
            sqlCommand.Parameters["@SearchKeyword"].Value = SearchKeyword;

            sqlCommand.Parameters.Add("@FriendlyTitle", SqlDbType.NVarChar);
            sqlCommand.Parameters["@FriendlyTitle"].Value = FriendlyTitle;

            sqlCommand.Parameters.Add("@Source", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Source"].Value = Source;


            sqlCommand.Parameters.Add("@EcryptBody", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptBody"].Value = new RSACSPCls().Encrypt(pc, Body);

            sqlCommand.Parameters.Add("@EcryptShortContent", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptShortContent"].Value = new RSACSPCls().Encrypt(pc, ShortContent);

            sqlCommand.Parameters.Add("@EcryptSubject", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptSubject"].Value = new RSACSPCls().Encrypt(pc, Subject);

            sqlCommand.Parameters.Add("@EntryDate", SqlDbType.DateTime);
            sqlCommand.Parameters["@EntryDate"].Value = EntryDate;

            sqlCommand.Parameters.Add("@HasDocAttached", SqlDbType.Int);
            sqlCommand.Parameters["@HasDocAttached"].Value = HasDocAttached;

            //sqlCommand.Parameters.Add("@HasImageAttached", SqlDbType.Int);
            //sqlCommand.Parameters["@HasImageAttached"].Value = HasImageAttached;

            //sqlCommand.Parameters.Add("@HasVideoAttached", SqlDbType.Int);
            //sqlCommand.Parameters["@HasVideoAttached"].Value = HasVideoAttached;

            //sqlCommand.Parameters.Add("@EmbVideo", SqlDbType.NVarChar);
            //sqlCommand.Parameters["@EmbVideo"].Value = EmbVideo;

            //sqlCommand.Parameters.Add("@EmbAudio", SqlDbType.NVarChar);
            //sqlCommand.Parameters["@EmbAudio"].Value = EmbAudio;


            try
            {
                sqlCommand.ExecuteNonQuery();
                string PreviewUrl = Screen.BuildViewUrl(new ParamCls[]{
                    new ParamCls("WebPartId",new NewsPreview().WebPartId)
                });
                //start workflow
                bool UseWorkflowTask = false;
                try
                {
                    UseWorkflowTask = bool.Parse(Environments.GetAppSettings("UseWorkflowTask"));
                }
                catch { }
                if (UseWorkflowTask)
                {
                    string FileWorkFlow = Environment.Environments.AdminPathRoot + "\\WorkFlows\\News.xml";
                    string WorkflowActionId = System.Guid.NewGuid().ToString();
                    new WorkFlowLibrary.WorkFlowApp(FileWorkFlow, Environment.Environments.ConnectionString).StartWorkFlow(
                        sqlTransaction,
                        NewsId,
                        NewsId,
                        Environment.Environments.CurrentLoginUser.LoginName,
                        Environment.Environments.CurrentLoginUser.LoginName,
                        Environment.Environments.CurrentLoginUser.OfficeCode,
                        NewsId,
                        Subject,
                        "",
                        PreviewUrl,
                        0);
                }
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
            return NewsId;
        }



        public void UpdateImage(
            SqlTransaction sqlTransaction,
            string NewsId,
            string belongSite,
            string UploadFileLarge,
            string UploadFileSmall)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableNews set ImageUrl=@ImageUrl,SmallImage=@SmallImage where NewsId=@NewsId;" +
                           " Insert into TableNewsHistory(NewsHistoryId,fk_News_Id,LogDate, LogUser, LogFullName) values (newid(),@NewsId,getdate(), @LogUser, @LogFullName)";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            string ln = new System.IO.FileInfo(UploadFileLarge).Name;
            sqlCommand.Parameters.Add("@ImageUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@ImageUrl"].Value = ln;

            string ls = new System.IO.FileInfo(UploadFileSmall).Name;
            sqlCommand.Parameters.Add("@SmallImage", SqlDbType.NVarChar);
            sqlCommand.Parameters["@SmallImage"].Value = ls;

            sqlCommand.Parameters.Add("@LogUser", SqlDbType.NVarChar);
            sqlCommand.Parameters["@LogUser"].Value = Environments.CurrentLoginUser.LoginName;

            sqlCommand.Parameters.Add("@LogFullName", SqlDbType.NVarChar);
            sqlCommand.Parameters["@LogFullName"].Value = Environments.CurrentLoginUser.FullName;

            try
            {
                sqlCommand.ExecuteNonQuery();
                string UploadFileName = "";
                byte[] b = null;
                if (ln.Trim().Length > 0)
                {
                    string FileUpload = Environments.WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + belongSite + "\\" + UploadFileLarge;
                    b = Environments.GetBytesFromImageFile(UploadFileLarge);
                    UploadFileName = new System.IO.FileInfo(UploadFileLarge).Name;
                }

                string UploadFileNameSmall = "";
                byte[] bsmall = null;
                if (ls.Trim().Length > 0)
                {
                    string FileUpload = Environments.WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + belongSite + "\\" + UploadFileSmall;
                    bsmall = Environments.GetBytesFromImageFile(UploadFileSmall);
                    UploadFileNameSmall = new System.IO.FileInfo(UploadFileSmall).Name;
                }

                //if (Environments.UseFrontEndDatabase)
                //{
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        WServices[iIndexService].PutImageNews(b, bsmall, Environments.PortalAlias, belongSite, "News", NewsId, UploadFileName, UploadFileNameSmall);
                //    }
                //}

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }



        public void UpdateLargeImage(
           SqlTransaction sqlTransaction,
           string NewsId,
           string belongSite,
           string UploadFile)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableNews set ImageUrl=@ImageUrl where NewsId=@NewsId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            string ln = new System.IO.FileInfo(UploadFile).Name;
            sqlCommand.Parameters.Add("@ImageUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@ImageUrl"].Value = ln;


            try
            {
                sqlCommand.ExecuteNonQuery();
                string UploadFileName = "";
                byte[] b = null;
                if (ln.Trim().Length > 0)
                {
                    string FileUpload = Environments.WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + belongSite + "\\" + UploadFile;
                    b = Environments.GetBytesFromImageFile(UploadFile);
                    UploadFileName = new System.IO.FileInfo(UploadFile).Name;
                }


                //if (Environments.UseFrontEndDatabase)
                //{
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        WServices[iIndexService].PutImageNewsLarge(b, Environments.PortalAlias, belongSite, "News", NewsId, UploadFileName);
                //    }
                //}

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }





        public void UpdateSmallImage(
           SqlTransaction sqlTransaction,
           string NewsId,
           string belongSite,
           string UploadFile)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = " Update TableNews set SmallImage=@SmallImage where NewsId=@NewsId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            string ln = new System.IO.FileInfo(UploadFile).Name;
            sqlCommand.Parameters.Add("@SmallImage", SqlDbType.NVarChar);
            sqlCommand.Parameters["@SmallImage"].Value = ln;


            try
            {
                sqlCommand.ExecuteNonQuery();
                string UploadFileName = "";
                byte[] b = null;
                if (ln.Trim().Length > 0)
                {
                    string FileUpload = Environments.WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + belongSite + "\\" + UploadFile;
                    b = Environments.GetBytesFromImageFile(UploadFile);
                    UploadFileName = new System.IO.FileInfo(UploadFile).Name;
                }

                //if (Environments.UseFrontEndDatabase)
                //{
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        WServices[iIndexService].PutImageNewsSmall(b, Environments.PortalAlias, belongSite, "News", NewsId, UploadFileName);
                //    }
                //}

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void SaveNews(
            SqlTransaction sqlTransaction,
            string ActiveLanguage,
            string GroupNewsItemId,
            string NewsId,
            string Subject,
            string ImageUrl,
            string ShortContent,
            string Body,
            string SearchKeyword,
            string Source,
            DateTime EntryDate,
            int HasDocAttached,
            int HasVideoAttached,
            int HasImageAttached,
            string EmbVideo,
            string EmbAudio)
        {
            bool HasCommit = false;
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string FriendlyTitle = Environments.ConvertSubject(Subject);

            string Query = " Update TableNews set fk_GroupNewsItem_Id=@GroupNewsItemId, EntryDate=@EntryDate,Source=@Source,FriendlyTitle=@FriendlyTitle,SearchKeyword=@SearchKeyword,[" + ActiveLanguage + "Subject]=@Subject,ImageUrl=@ImageUrl,[" + ActiveLanguage + "ShortContent]=@ShortContent,[" + ActiveLanguage + "Body]=@Body,[" + ActiveLanguage + "EcryptBody]=@EcryptBody,[" + ActiveLanguage + "EcryptShortContent]=@EcryptShortContent,[" + ActiveLanguage + "EcryptSubject]=@EcryptSubject, HasDocAttached=@HasDocAttached, HasVideoAttached=@HasVideoAttached,HasImageAttached=@HasImageAttached,EmbVideo=@EmbVideo,EmbAudio=@EmbAudio where NewsId=@NewsId;" +
                           " Insert into TableNewsHistory(NewsHistoryId, fk_News_Id, LogDate, LogUser, LogFullName) values (newid(),@NewsId,GetDate(),@LogUser,@LogFullName)";         

            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject;

            sqlCommand.Parameters.Add("@Source", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Source"].Value = Source;

            sqlCommand.Parameters.Add("@EnycContent", SqlDbType.NText);
            sqlCommand.Parameters["@EnycContent"].Value = new RSACSPCls().Encrypt(pc, Body);


            sqlCommand.Parameters.Add("@SearchKeyword", SqlDbType.NVarChar);
            sqlCommand.Parameters["@SearchKeyword"].Value = SearchKeyword;

            sqlCommand.Parameters.Add("@ImageUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@ImageUrl"].Value = ImageUrl;


            sqlCommand.Parameters.Add("@ShortContent", SqlDbType.NText);
            sqlCommand.Parameters["@ShortContent"].Value = ShortContent;

            sqlCommand.Parameters.Add("@FriendlyTitle", SqlDbType.NVarChar);
            sqlCommand.Parameters["@FriendlyTitle"].Value = FriendlyTitle;

            sqlCommand.Parameters.Add("@Body", SqlDbType.NText);
            sqlCommand.Parameters["@Body"].Value = Body;

            sqlCommand.Parameters.Add("@EcryptBody", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptBody"].Value = new RSACSPCls().Encrypt(pc, Body);

            sqlCommand.Parameters.Add("@EcryptShortContent", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptShortContent"].Value = new RSACSPCls().Encrypt(pc, ShortContent);

            sqlCommand.Parameters.Add("@EcryptSubject", SqlDbType.NText);
            sqlCommand.Parameters["@EcryptSubject"].Value = new RSACSPCls().Encrypt(pc, Subject);

            sqlCommand.Parameters.Add("@EntryDate", SqlDbType.DateTime);
            sqlCommand.Parameters["@EntryDate"].Value = EntryDate;

            sqlCommand.Parameters.Add("@LogUser", SqlDbType.VarChar);
            sqlCommand.Parameters["@LogUser"].Value = Environments.CurrentLoginUser.LoginName;

            sqlCommand.Parameters.Add("@LogFullName", SqlDbType.NVarChar);
            sqlCommand.Parameters["@LogFullName"].Value = Environments.CurrentLoginUser.FullName;


            sqlCommand.Parameters.Add("@HasDocAttached", SqlDbType.Int);
            sqlCommand.Parameters["@HasDocAttached"].Value = HasDocAttached;

            sqlCommand.Parameters.Add("@HasImageAttached", SqlDbType.Int);
            sqlCommand.Parameters["@HasImageAttached"].Value = HasImageAttached;

            sqlCommand.Parameters.Add("@HasVideoAttached", SqlDbType.Int);
            sqlCommand.Parameters["@HasVideoAttached"].Value = HasVideoAttached;


            sqlCommand.Parameters.Add("@EmbVideo", SqlDbType.NVarChar);
            sqlCommand.Parameters["@EmbVideo"].Value = EmbVideo.Trim().Length > 0?EmbVideo:"";

            sqlCommand.Parameters.Add("@EmbAudio", SqlDbType.NVarChar);
            sqlCommand.Parameters["@EmbAudio"].Value = EmbAudio.Trim().Length > 0?EmbAudio:"";


            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                    HasCommit = true;
                }


                //DataSet ds = new DataSet();
                //Query = " select *,[ActiveLanguage]=@ActiveLanguage from TableNews where NewsId=@NewsId;";
                //SqlConnection TempSqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                //TempSqlConnection.Open();
                //SqlCommand TempSqlCommand = TempSqlConnection.CreateCommand();
                //TempSqlCommand.CommandText = Query;
                //TempSqlCommand.Parameters.Clear();
                //TempSqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
                //TempSqlCommand.Parameters["@NewsId"].Value = NewsId;

                //TempSqlCommand.Parameters.Add("@ActiveLanguage", SqlDbType.VarChar);
                //TempSqlCommand.Parameters["@ActiveLanguage"].Value = ActiveLanguage;
                //SqlDataAdapter TempSqlda = new SqlDataAdapter(TempSqlCommand);
                //TempSqlda.Fill(ds);


                //ds.Tables[0].TableName = "TableNews";
                //ds.Tables[0].Rows[0]["fk_GroupNewsItem_Id"] = GroupNewsItemId;
                //string XmlData = ds.GetXml();
                //if (Environments.UseFrontEndDatabase)
                //{
                //    Environment.FrontEndWebService.asmx.Service[] WServices=FrontendWebServiceController.CreateWebService();
                //    for (int iIndexService = 0; iIndexService < WServices.Length; iIndexService++)
                //    {
                //        string RetValue = WServices[iIndexService].SaveNews(Environments.SecurityCode, XmlData);
                //        if (RetValue.Trim().Length > 0)
                //        {
                //            throw new Exception(RetValue);
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    if (HasCommit == false)
                    {
                        sqlTransaction.Rollback();
                    }
                }
                throw (ex);
            }
        }


        public void SetOnTop(
           SqlTransaction sqlTransaction,
           string GroupNewsItemId,
           string NewsId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = //" Update TableNews set ShowOnTop=0 where fk_GroupNewsItem_Id=@GroupNewsItemId;" +
                           " Update TableNews set ShowOnTop=1 where NewsId=@NewsId;";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            //sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            //sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;

            try
            {
                sqlCommand.ExecuteNonQuery();


                DataSet ds = new DataSet("xml");
                ds.Tables.Add("dtNews");
                ds.Tables[0].Columns.Add("GroupNewsItemId");
                ds.Tables[0].Columns.Add("NewsId");
                ds.Tables[0].Rows.Add(new object[]{
                    GroupNewsItemId,
                    NewsId
                });
                string XmlData = ds.GetXml();                

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }

        public void DisOnTop(
          SqlTransaction sqlTransaction,
          string GroupNewsItemId,
          string NewsId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = //" Update TableNews set ShowOnTop=0 where fk_GroupNewsItem_Id=@GroupNewsItemId;" +
                           " Update TableNews set ShowOnTop=0 where NewsId=@NewsId;";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            //sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            //sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;

            try
            {
                sqlCommand.ExecuteNonQuery();


                DataSet ds = new DataSet("xml");
                ds.Tables.Add("dtNews");
                ds.Tables[0].Columns.Add("GroupNewsItemId");
                ds.Tables[0].Columns.Add("NewsId");
                ds.Tables[0].Rows.Add(new object[]{
                    GroupNewsItemId,
                    NewsId
                });
                string XmlData = ds.GetXml();                

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }

        public void SyncNews(string NewsId)
        {
            string WsUrl = Environments.GetConfigFromXml("AppWs");
            string WsSecurityCode = Portal.Environment.Environments.WsSecurityCode;
            bool UseWsForDeploy = Environments.UseWsForDeploy;
            SyncNews(
                Environment.Environments.ConnectionString,
                NewsId,
                UseWsForDeploy,
                Environments.ActiveLanguage,
                Environments.PathRoot,
                Environments.WorkingPath,
                Portal.Environment.Environments.PortalAlias,
                WsUrl,
                WsSecurityCode);
        }



        public void SyncNews(
            string ConnectionString,
            string NewsId,
            bool UseWsForDeploy,
            string ActiveLanguage,
            string PathRoot,
            string WorkingPath,
            string PortalAlias,
            string WsUrl,
            string WsSecurityCode)
        {
            if (UseWsForDeploy)
            {
                NewsCls NewsCls = CreateModel(ConnectionString, ActiveLanguage, NewsId);
                DataSet ds = new DataSet();

                string Query =
                    " select * from TableGroupNews where GroupNewsId in (select fk_GroupNews_Id from TableNews where NewsId=@NewsId);" +
                    " select * from TableGroupNewsItem where GroupNewsItemId in (select fk_GroupNewsItem_Id from TableNews where NewsId=@NewsId);" +
                    " select * from TableNews where NewsId=@NewsId;";
                SqlConnection sqlConnectionCheck = new SqlConnection(ConnectionString);
                sqlConnectionCheck.Open();
                SqlCommand sqlCommandCheck = sqlConnectionCheck.CreateCommand();
                sqlCommandCheck.CommandText = Query;
                sqlCommandCheck.Parameters.Clear();
                sqlCommandCheck.Parameters.Add("@NewsId", SqlDbType.VarChar);
                sqlCommandCheck.Parameters["@NewsId"].Value = NewsId;

                SqlDataAdapter sqlda = new SqlDataAdapter(sqlCommandCheck);
                sqlda.Fill(ds);

                ds.Tables[0].TableName = "TableGroupNews";
                ds.Tables[1].TableName = "TableGroupNewsItem";
                ds.Tables[2].TableName = "TableNews";
                ds.Tables[2].Rows[0]["IsPublishing"] = NewsCls.IsPublishing;
                if (NewsCls.IsPublishing == 1)
                {
                    ds.Tables[2].Rows[0]["WorkflowStatus"] = "Approved";
                }
                else
                {
                    ds.Tables[2].Rows[0]["WorkflowStatus"] = "disabled";
                }

                byte[] oBytes1 = null;
                byte[] oBytes2 = null;
                byte[] oBytes3 = null;
                string ImageUrl = "";
                try
                {
                    ImageUrl = (string)ds.Tables["TableNews"].Rows[0]["ImageUrl"];
                }
                catch { }
                string SmallImage = "";
                try
                {
                    SmallImage = (string)ds.Tables["TableNews"].Rows[0]["SmallImage"];
                }
                catch { }
                if (ImageUrl.Trim().Length > 0)
                {
                    string LoadFileImage =
                        WorkingPath + "\\Media\\" + PortalAlias + "\\News\\Images\\" + NewsCls.belongSite + "\\" + ImageUrl;
                    if (new System.IO.FileInfo(LoadFileImage).Exists)
                    {
                        oBytes1 = Portal.Environment.Environments.GetBytesFromFile(LoadFileImage);
                    }
                }
                if (SmallImage.Trim().Length > 0)
                {
                    string LoadSmallImage =
                        WorkingPath + "\\Media\\" + PortalAlias + "\\News\\Images\\" + NewsCls.belongSite + "\\" + SmallImage;
                    if (new System.IO.FileInfo(LoadSmallImage).Exists)
                    {
                        oBytes2 = Environments.GetBytesFromFile(LoadSmallImage);
                    }
                }

                string ShortContent = (string)ds.Tables["TableNews"].Rows[0][ActiveLanguage + "ShortContent"];
                string Body = (string)ds.Tables["TableNews"].Rows[0][ActiveLanguage + "Body"];
                Body = Portal.Environment.ReplateTextBll.ReplaceText(PathRoot, PortalAlias, Body);
                ds.Tables["TableNews"].Rows[0][ActiveLanguage + "ShortContent"] = ShortContent;
                ds.Tables["TableNews"].Rows[0][ActiveLanguage + "Body"] = Body;

                oBytes3 = BobController.ReadBlobField(ConnectionString, "TableNews", "DocumentContent", "NewsId", NewsId);
                string XmlData = ds.GetXml();
                Framework.AppWs.WsReturnParamCls oWsReturnParamCls = Environments.CreateAppWs(WsUrl).SyncData(
                    WsSecurityCode,
                    NewsId,
                    NewsCls.belongSite,
                    XmlData,
                    oBytes1,
                    oBytes2,
                    oBytes3,
                    Framework.AppWs.ESycDataType.News);
                if (oWsReturnParamCls.Error)
                {
                    throw new Exception("WS: " + oWsReturnParamCls.ErrorMsg);
                }


            }
        }

        public void PublishingPublicWebSite(string NewsId)
        {
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
            SqlCommand sqlCommand = sqlTransaction.Connection.CreateCommand();
            sqlCommand.Transaction = sqlTransaction;

            sqlCommand.CommandText = " Update TableNews set WorkflowStatus='Approved',IsPublishing=1, EntryDate = GETDATE() where NewsId='" + NewsId + "' ";
            try
            {
                sqlCommand.ExecuteNonQuery();
                sqlTransaction.Commit();
            }
            catch (Exception ex)
            {
                sqlTransaction.Rollback();
                throw (ex);
            }
        }


        public void UnPublishingPublicWebSite(string NewsId)
        {
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
            SqlCommand sqlCommand = sqlTransaction.Connection.CreateCommand();
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.CommandText = " Update TableNews set WorkflowStatus='Disable',IsPublishing=0 where NewsId='" + NewsId + "'";
            try
            {
                sqlCommand.ExecuteNonQuery();
                sqlTransaction.Commit();
            }
            catch (Exception ex)
            {
                sqlTransaction.Rollback();
                throw (ex);
            }
        }

        public void DeleteNews(SqlTransaction sqlTransaction, string NewsId)
        {
            NewsCls oNewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            Environment.Permission.DeletePermission(sqlTransaction, NewsId);
            string Query =
                           " Delete from TableNewsHistory where fk_News_Id=@NewsId;" +
                           " Delete from TableNews where NewsId=@NewsId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            try
            {
                sqlCommand.ExecuteNonQuery();
                string SubjectDeleted = "";
                if (oNewsCls != null)
                {
                    SubjectDeleted = oNewsCls.Subject;
                }
                Environments.AddDeletedRecordForSync(sqlTransaction, "TableNews", "Tin tức sự kiện", NewsId, SubjectDeleted);
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void ResetDocumentContent(SqlTransaction sqlTransaction, string NewsId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query =
                           " Update TableNews set DocumentContent=null where NewsId=@NewsId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            try
            {
                sqlCommand.ExecuteNonQuery();
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void RemoveSelectedDocumentContent(SqlTransaction sqlTransaction, string NewsId, string AttachmentId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query =
                      " Delete from TableNewsAttachmentsDetail where fk_NewsId=@fk_NewsId and fk_AttachmentId = @fk_AttachmentId ;" +
                      " Update TableAttachments set DocumentContent=null where AttachmentId=@fk_AttachmentId";

            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            sqlCommand.Parameters.Add("@fk_NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_NewsId"].Value = NewsId;

            sqlCommand.Parameters.Add("@fk_AttachmentId", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_AttachmentId"].Value = AttachmentId;

            try
            {
                sqlCommand.ExecuteNonQuery();
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }




        public void DeleteNewsInGroupNews(SqlTransaction sqlTransaction, string GroupNewsItemId)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }


            string Query =
                           " Delete from TableNews where fk_GroupNewsItem_Id=@GroupNewsItemId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@GroupNewsItemId", SqlDbType.VarChar);
            sqlCommand.Parameters["@GroupNewsItemId"].Value = GroupNewsItemId;

            try
            {
                sqlCommand.ExecuteNonQuery();
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }

        public void UploadDocument(
            SqlTransaction sqlTransaction,
            string belongSite,
            string NewsId,
            string UploadFile)
        {
            byte[] b = null;
            if (UploadFile.Trim().Length > 0)
            {
                b = Environments.GetBytesFromImageFile(UploadFile);
            }

            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query =
                           " Update TableNews set DocumentContent=@DocumentContent,UploadFileName=@UploadFileName where NewsId=@NewsId";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            sqlCommand.Parameters.Add("@UploadFileName", SqlDbType.NVarChar);
            if (b != null)
            {
                sqlCommand.Parameters["@UploadFileName"].Value = new System.IO.FileInfo(UploadFile).Name;
            }
            else
            {
                sqlCommand.Parameters["@UploadFileName"].Value = DBNull.Value;
            }


            sqlCommand.Parameters.Add("@DocumentContent", SqlDbType.Image);
            if (b != null)
            {
                sqlCommand.Parameters["@DocumentContent"].Value = b;
            }
            else
            {
                sqlCommand.Parameters["@DocumentContent"].Value = DBNull.Value;
            }

            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public void UploadMultiDocument(
            SqlTransaction sqlTransaction,
            string belongSite,
            string NewsId,
            string UploadFile,
            string Subject,
            string FriendlyTitle,
            int SortIndex
            )
        {
            byte[] b = null;
            if (UploadFile.Trim().Length > 0)
            {
                b = Environments.GetBytesFromImageFile(UploadFile);
            }

            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query =
                           " Insert into TableAttachments(AttachmentId,Subject,FriendlyTitle,DocumentContent,UploadFileName,EntryDate,IsPublishing) values (@AttachmentId,@Subject,@FriendlyTitle,@DocumentContent,@UploadFileName,@EntryDate,@IsPublishing) ;" +
                           " Insert into TableNewsAttachmentsDetail(NewsAttachsId,fk_NewsId,fk_AttachmentId,SortIndex) values (@NewsAttachsId,@fk_NewsId,@fk_AttachmentId,@SortIndex) ";

            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();

            string AttachmentId = System.Guid.NewGuid().ToString().ToUpper();

            sqlCommand.Parameters.Add("@AttachmentId", SqlDbType.VarChar);
            sqlCommand.Parameters["@AttachmentId"].Value = AttachmentId; 

            sqlCommand.Parameters.Add("@Subject", SqlDbType.NVarChar);
            sqlCommand.Parameters["@Subject"].Value = Subject ;

            sqlCommand.Parameters.Add("@FriendlyTitle", SqlDbType.NVarChar);
            sqlCommand.Parameters["@FriendlyTitle"].Value = FriendlyTitle;

            sqlCommand.Parameters.Add("@EntryDate", SqlDbType.DateTime);
            sqlCommand.Parameters["@EntryDate"].Value = DateTime.Now;

            sqlCommand.Parameters.Add("@IsPublishing", SqlDbType.Bit);
            sqlCommand.Parameters["@IsPublishing"].Value = true;
            
            sqlCommand.Parameters.Add("@UploadFileName", SqlDbType.NVarChar);
            if (b != null)
            {
                sqlCommand.Parameters["@UploadFileName"].Value = new System.IO.FileInfo(UploadFile).Name;
            }
            else
            {
                sqlCommand.Parameters["@UploadFileName"].Value = DBNull.Value;
            }


            sqlCommand.Parameters.Add("@DocumentContent", SqlDbType.Image);
            if (b != null)
            {
                sqlCommand.Parameters["@DocumentContent"].Value = b;
            }
            else
            {
                sqlCommand.Parameters["@DocumentContent"].Value = DBNull.Value;
            }



            sqlCommand.Parameters.Add("@NewsAttachsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsAttachsId"].Value = System.Guid.NewGuid().ToString().ToUpper(); ;

            sqlCommand.Parameters.Add("@fk_NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_NewsId"].Value = NewsId;

            sqlCommand.Parameters.Add("@fk_AttachmentId", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_AttachmentId"].Value = AttachmentId;            

            sqlCommand.Parameters.Add("@SortIndex", SqlDbType.SmallInt);
            sqlCommand.Parameters["@SortIndex"].Value = SortIndex;


            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }


        public DataTable ReadingNewsRelation(string NewsId)
        {
            string Query = " select TableNewsRelation.*,ShortId=TableNews.ShortId, EntryDate,Subject=TableNews.[" + Environments.ActiveLanguage + "Subject] from TableNewsRelation " +
                           "        join TableNews on TableNews.NewsId=fk_RelationNews_Id" +
                           " where fk_SrcNews_Id=@NewsId and IsPublishing=1 order by TableNewsRelation.SortIndex";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }



        public DataTable ReadingAttachmentsNews(string NewsId)
        {
            string Query = " select TableNewsAttachmentsDetail.*,NewsId=TableNews.NewsId,TableAttachments.FriendlyTitle, TableAttachments.EntryDate,TableAttachments.Subject,TableAttachments.UploadFileName from TableNewsAttachmentsDetail " +
                           " join TableNews on TableNews.NewsId=TableNewsAttachmentsDetail.fk_NewsId " +
                           " join TableAttachments on TableAttachments.AttachmentId=TableNewsAttachmentsDetail.fk_AttachmentId " +
                           //" where fk_NewsId=@NewsId and TableNews.IsPublishing=1 " +
                           " where fk_NewsId=@NewsId " +
                           " order by TableNewsAttachmentsDetail.SortIndex";

            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@NewsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsId"].Value = NewsId;

            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }


        public void AddNewsRelation(string SrcNewsId, string NewsId)
        {
            string Query =
                " Delete from TableNewsRelation where fk_SrcNews_Id=@fk_SrcNews_Id and fk_RelationNews_Id=@fk_RelationNews_Id;" +
                " Insert into TableNewsRelation(NewsRelationId, fk_SrcNews_Id, fk_RelationNews_Id) values (@NewsRelationId, @fk_SrcNews_Id, @fk_RelationNews_Id)";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;

            string NewsRelationId = System.Guid.NewGuid().ToString();

            sqlCommand.Parameters.Add("@NewsRelationId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsRelationId"].Value = NewsRelationId;

            sqlCommand.Parameters.Add("@fk_SrcNews_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_SrcNews_Id"].Value = SrcNewsId;

            sqlCommand.Parameters.Add("@fk_RelationNews_Id", SqlDbType.VarChar);
            sqlCommand.Parameters["@fk_RelationNews_Id"].Value = NewsId;

            sqlCommand.ExecuteNonQuery();
        }


        public void RemoveNewsRelation(string NewsRelationId)
        {
            string Query =
                " Delete from TableNewsRelation where NewsRelationId=@NewsRelationId";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;


            sqlCommand.Parameters.Add("@NewsRelationId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsRelationId"].Value = NewsRelationId;

            sqlCommand.ExecuteNonQuery();
        }



        public void UpdateNewsRelationSortIndex(string NewsRelationId, string SortIndex)
        {
            string Query =
                " Update TableNewsRelation set SortIndex=@SortIndex where NewsRelationId=@NewsRelationId";
            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;

            sqlCommand.Parameters.Add("@NewsRelationId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsRelationId"].Value = NewsRelationId;

            sqlCommand.Parameters.Add("@SortIndex", SqlDbType.Int);
            sqlCommand.Parameters["@SortIndex"].Value = SortIndex;

            sqlCommand.ExecuteNonQuery();
        }

        public void UpdateNewsAttachmentsSortIndex(string NewsAttachsId, string SortIndex)
        {
            string Query =
                " Update TableNewsAttachmentsDetail set SortIndex=@SortIndex where NewsAttachsId=@NewsAttachsId ";

            SqlConnection sqlConnection = new SqlConnection(Environment.Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;

            sqlCommand.Parameters.Add("@NewsAttachsId", SqlDbType.VarChar);
            sqlCommand.Parameters["@NewsAttachsId"].Value = NewsAttachsId;

            sqlCommand.Parameters.Add("@SortIndex", SqlDbType.Int);
            sqlCommand.Parameters["@SortIndex"].Value = int.Parse(SortIndex);

            sqlCommand.ExecuteNonQuery();
        }
    }
}


